package main

import "fmt"

func f1() int {
	x := 5
	defer func() {
		x++
	}()
	return x
}

func f2() (x int) {
	defer func() {
		x++
	}()
	return 5
}

func f3() (y int) {
	x := 5
	defer func() {
		y++
	}()
	return x
}
func f4() (y int) {
	defer func(x int) {
		y++
	}(y)
	return 5
}

func calc(index string, a, b int) int {
	ret := a + b
	fmt.Println(index, a, b, ret)
	return ret
}

/*
A 1 2 3
B 10 2 12
BB 10 12 22
AA 1 3 4
*/
/* func main() {
	x := 1
	y := 2
	defer calc("AA", x, calc("A", x, y))
	x = 10
	defer calc("BB", x, calc("B", x, y))
	y = 20
}
*/

// panic/revover
// 异常处理
func funA() {

	fmt.Println("a")

}

func funB() {
	// 程序的最后使用recover捕获可能发生的异常，类似try()catch{}块

	defer func() {
		err := recover()
		// 代表有异常了
		if err != nil {
			fmt.Println("recover in B")
		}
	}()

	// 模拟一个可能发生的异常
	panic("panic in B")

}

func funC() {
	fmt.Println("C")
}

func main() {
	funA()
	funB()
	funC()
}
